﻿using Saisie_des_prélèvements.Models;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Saisie_des_prélèvements.ViewModels
{
    public class TakenViewModel : ViewModelBase
    {
        #region Properties

        private int taken_id = 0;
        public int TakenId
        {
            get
            { return taken_id; }

            set
            {
                if (taken_id == value)
                { return; }

                taken_id = value;
                RaisePropertyChanged("TakenId");
            }
        }

        private string prod_code = string.Empty;
        public string ProductCode
        {
            get
            { return prod_code; }

            set
            {
                if (prod_code == value)
                { return; }

                prod_code = value;
                RaisePropertyChanged("ProductCode");
            }
        }

        private string taken_type = string.Empty;
        public string TakenType
        {
            get
            { return taken_type; }

            set
            {
                if (taken_type == value)
                { return; }

                taken_type = value;
                RaisePropertyChanged("TakenType");
            }
        }

        private string taken_code = string.Empty;
        public string TakenCode
        {
            get
            { return taken_code; }

            set
            {
                if (taken_code == value)
                { return; }

                taken_code = value;
                RaisePropertyChanged("TakenCode");
            }
        }

        private int? client_taken_id;
        public int? ClientTakenId
        {
            get
            { return client_taken_id; }

            set
            {
                if (client_taken_id == value)
                { return; }

                client_taken_id = value;
                RaisePropertyChanged("ClientTakenID");
            }
        }

        private string surv_code = string.Empty;
        public string SurvCode
        {
            get
            { return surv_code; }

            set
            {
                if (surv_code == value)
                { return; }

                surv_code = value;
                RaisePropertyChanged("SurvCode");
            }
        }

        private string taken_point_code = string.Empty;
        public string TakenPointCode
        {
            get
            { return taken_point_code; }

            set
            {
                if (taken_point_code == value)
                { return; }

                taken_point_code = value;
                RaisePropertyChanged("TakenPointCode");
            }
        }

        private string taken_point_label = string.Empty;
        public string TakenPointLabel
        {
            get
            { return taken_point_label; }

            set
            {
                if (taken_point_label == value)
                { return; }

                taken_point_label = value;
                RaisePropertyChanged("TakenPointLabel");
            }
        }

        private bool? taken_point_modifiable = false;
        public bool? TalkenPointMOdifiable
        {
            get
            { return taken_point_modifiable; }

            set
            {
                if (taken_point_modifiable == value)
                { return; }

                taken_point_modifiable = value;
                RaisePropertyChanged("TalkenPointMOdifiable");
            }
        }

        private DateTime? taken_date;
        public DateTime? TakenDate
        {
            get
            { return taken_date; }

            set
            {
                if (taken_date == value)
                { return; }

                taken_date = value;
                RaisePropertyChanged("TakenDate");
            }
        }

        private string days_taken;
        public string TakenDay
        {
            get
            { return days_taken; }

            set
            {
                if (days_taken == value)
                { return; }

                days_taken = value;
                RaisePropertyChanged("TakenDay");
            }
        }

        private string months_taken = string.Empty;
        public string TakenMonth
        {
            get
            { return months_taken; }

            set
            {
                if (months_taken == value)
                { return; }

                months_taken = value;
                RaisePropertyChanged("TakenMonth");
            }
        }

        private string years_taken = string.Empty;
        public string TakenYear
        {
            get
            { return years_taken; }

            set
            {
                if (years_taken == value)
                { return; }

                years_taken = value;
                RaisePropertyChanged("TakenYear");
            }
        }

        private string taken_comment = string.Empty;
        public string TakenComment
        {
            get
            { return taken_comment; }

            set
            {
                if (taken_comment == value)
                { return; }

                taken_comment = value;
                RaisePropertyChanged("TakenComment");
            }
        }

        private int? taken_point_day;
        public int? TakenPointDate
        {
            get
            { return taken_point_day; }

            set
            {
                if (taken_point_day == value)
                { return; }

                taken_point_day = value;
                RaisePropertyChanged("TakenPointDate");
            }
        }

        private DateTime? taken_date_next;
        public DateTime? TakenDateNext
        {
            get
            { return taken_date_next; }

            set
            {
                if (taken_date_next == value)
                { return; }

                taken_date_next = value;
                RaisePropertyChanged("TakenDateNext");
            }
        }

        private DateTime? taken_date_last;
        public DateTime? TakenDateLast
        {
            get
            { return taken_date_last; }

            set
            {
                if (taken_date_last == value)
                { return; }

                taken_date_last = value;
                RaisePropertyChanged("TakenDateLast");
            }
        }

        private string amount = string.Empty;
        public string Amount
        {
            get
            { return amount; }

            set
            {
                if (amount == value)
                { return; }

                amount = value;
                RaisePropertyChanged("Amount");
            }
        }

        private string sample_number = string.Empty;
        public string SampleNumber
        {
            get
            { return sample_number; }

            set
            {
                if (sample_number == value)
                { return; }

                sample_number = value;
                RaisePropertyChanged("SampleNumber");
            }
        }

        private string comment = string.Empty;
        public string Comment
        {
            get
            { return comment; }

            set
            {
                if (comment == value)
                { return; }

                comment = value;
                RaisePropertyChanged("SampleNumber");
            }
        }

        private string value_01 = string.Empty;
        public string Val01
        {
            get
            { return value_01; }

            set
            {
                if (value_01 == value)
                { return; }

                value_01 = value;
                RaisePropertyChanged("Val01");
            }
        }

        private string value_02 = string.Empty;
        public string Val02
        {
            get
            { return value_02; }

            set
            {
                if (value_02 == value)
                { return; }

                value_02 = value;
                RaisePropertyChanged("Val02");
            }
        }

        private string value_03 = string.Empty;
        public string Val03
        {
            get
            { return value_03; }

            set
            {
                if (value_03 == value)
                { return; }

                value_03 = value;
                RaisePropertyChanged("Val03");
            }
        }

        private string value_04 = string.Empty;
        public string Val04
        {
            get
            { return value_04; }

            set
            {
                if (value_04 == value)
                { return; }

                value_04 = value;
                RaisePropertyChanged("Val04");
            }
        }

        private string value_05 = string.Empty;
        public string Val05
        {
            get
            { return value_05; }

            set
            {
                if (value_05 == value)
                { return; }

                value_05 = value;
                RaisePropertyChanged("Val05");
            }
        }

        private string value_06 = string.Empty;
        public string Val06
        {
            get
            { return value_06; }

            set
            {
                if (value_06 == value)
                { return; }

                value_06 = value;
                RaisePropertyChanged("Val06");
            }
        }

        private string value_07 = string.Empty;
        public string Val07
        {
            get
            { return value_07; }

            set
            {
                if (value_07 == value)
                { return; }

                value_07 = value;
                RaisePropertyChanged("Val07");
            }
        }

        private string value_08 = string.Empty;
        public string Val08
        {
            get
            { return value_08; }

            set
            {
                if (value_08 == value)
                { return; }

                value_08 = value;
                RaisePropertyChanged("Val08");
            }
        }

        private string value_09 = string.Empty;
        public string Val09
        {
            get
            { return value_09; }

            set
            {
                if (value_09 == value)
                { return; }

                value_09 = value;
                RaisePropertyChanged("Val09");
            }
        }

        private string value_10 = string.Empty;
        public string Val10
        {
            get
            { return value_10; }

            set
            {
                if (value_10 == value)
                { return; }

                value_10 = value;
                RaisePropertyChanged("Val10");
            }
        }

        private string value_11 = string.Empty;
        public string Val11
        {
            get
            { return value_11; }

            set
            {
                if (value_11 == value)
                { return; }

                value_11 = value;
                RaisePropertyChanged("Val11");
            }
        }

        private string value_12 = string.Empty;
        public string Val12
        {
            get
            { return value_12; }

            set
            {
                if (value_12 == value)
                { return; }

                value_12 = value;
                RaisePropertyChanged("Val12");
            }
        }

        private Nullable<DateTime> date_extraction;
        public Nullable<DateTime> DateExtraction
        {
            get
            { return date_extraction; }

            set
            {
                if (date_extraction == value)
                { return; }

                date_extraction = value;
                RaisePropertyChanged("DateExtraction");
            }
        }

        private string reason_delete = string.Empty;
        public string ReasonDelete
        {
            get
            { return reason_delete; }

            set
            {
                if (reason_delete == value)
                { return; }

                reason_delete = value;
                RaisePropertyChanged("ReasonDelete");
            }
        }

        private byte status = 0;
        public byte Status
        {
            get
            { return status; }

            set
            {
                if (status == value)
                { return; }

                status = value;
                RaisePropertyChanged("Status");
            }
        }

        private int? user_create = 0;
        public int? UserCreate
        {
            get
            { return user_create; }

            set
            {
                if (user_create == value)
                { return; }

                user_create = value;
                RaisePropertyChanged("UserCreate");
            }
        }

        private DateTime? date_create = System.DateTime.Today;
        public DateTime? DateCreate
        {
            get
            { return date_create; }

            set
            {
                if (date_create == value)
                { return; }

                date_create = value;
                RaisePropertyChanged("DateCreate");
            }
        }

        public string BGColor { get; set; }

        #endregion "Properties"

        public List<TakenViewModel> GetTakenInfoByClient(int ClientId)
        {

            var db = new SQLite.SQLiteConnection(App.DBPath);
            List<TakenViewModel> result = db.Query<TakenViewModel>(
                string.Format("SELECT taken_id TakenId ,prod_code ProductCode ,taken_type TakenType, taken_code TakenCode, client_taken_id ClientTakenId, surv_code SurvCode ,taken_point_code TakenPointCode,taken_point_label TakenPointLabel,taken_point_modifiable TalkenPointMOdifiable,taken_date TakenDate,days_taken TakenDay,months_taken TakenMonth,years_taken TakenYear,taken_comment TakenComment,user_create UserCreate,date_create DateCreate,taken_point_day TakenPointDate,taken_date_next TakenDateNext,taken_date_last TakenDateLast,amount Amount, CASE status WHEN 1 THEN '#FFFFA5000' ELSE '#FFFFFFFF' END Status FROM k_taken WHERE client_taken_id={0} ORDER BY prod_code", ClientId));

            return result;
        }

        public TakenViewModel GetTakenInfoLast()
        {
            var db = new SQLite.SQLiteConnection(App.DBPath);
            TakenViewModel result = new TakenViewModel();
            var r = db.Table<k_taken>().OrderByDescending(t => t.taken_id).First(); 

            result.TakenId = r.taken_id;
            result.ProductCode = r.prod_code;
            result.TakenType = r.taken_type.ToString();
            result.SurvCode = r.surv_code;
            result.TakenPointCode = r.taken_point_code;
            result.TakenPointLabel = r.taken_point_label;
            result.TalkenPointMOdifiable = r.taken_point_modifiable == 0 ? false : true;
            result.TakenDate = r.taken_date;
            result.TakenDay = r.days_taken;
            result.TakenMonth = r.months_taken;
            result.TakenYear = r.years_taken;
            result.TakenComment = r.taken_comment;
            result.TakenPointDate = r.taken_point_day;
            result.TakenDateNext = r.taken_date_next;
            result.TakenDateLast = r.taken_date_last;
            result.Amount = amount;
            result.Comment = r.comment;
            result.SampleNumber = r.sample_number;
            result.Val01 = r.value_01;
            result.Val02 = r.value_02;
            result.Val03 = r.value_03;
            result.Val04 = r.value_04;
            result.Val05 = r.value_05;
            result.Val06 = r.value_06;
            result.Val07 = r.value_07;
            result.Val08 = r.value_08;
            result.Val09 = r.value_09;
            result.Val10 = r.value_10;
            result.Val11 = r.value_11;
            result.Val12 = r.value_12;
            result.DateExtraction = r.date_extraction;
            result.ReasonDelete = r.reason_delete;
            result.Status = r.status;

            return result;
        }

        public List<TakenViewModel> GetTakenListByClient(int ClientId)
        {
            //string[] bgcolor = new string[] { "", "#FFFFA5000", "" };
            var db = new SQLite.SQLiteConnection(App.DBPath);
            List<TakenViewModel> result = db.Query<TakenViewModel>(
                string.Format("SELECT taken_id TakenId ,prod_code ProductCode, PRD_LIB TakenPointLabel, surv_code SurvCode, days_taken TakenDay, months_taken TakenMonth, years_taken TakenYear, CASE status WHEN 1 THEN '#ff7b21' WHEN 2 THEN '#ffffff' WHEN 3 THEN '#9bbb59' ELSE '#FFFFFFFF' END BGColor, status Status FROM k_taken JOIN NPRODUIT ON prod_code = PRD_CODE WHERE client_taken_id={0} ORDER BY prod_code", ClientId));

            return result;
        }

        public TakenViewModel GetTakenInfoById(int takenid)
        {
            var db = new SQLite.SQLiteConnection(App.DBPath);
            TakenViewModel result = new TakenViewModel();
            var r = db.Table<k_taken>().Where(t=> t.taken_id == takenid).First();

            result.TakenId = r.taken_id;
            result.ProductCode = r.prod_code;
            result.TakenCode = r.taken_code;
            result.ClientTakenId = r.client_taken_id;
            result.TakenType = r.taken_type.ToString();
            result.SurvCode = r.surv_code;
            result.TakenPointCode = r.taken_point_code;
            result.TakenPointLabel = r.taken_point_label;
            result.TalkenPointMOdifiable = r.taken_point_modifiable == 0 ? false : true;
            result.TakenDate = r.taken_date;
            result.TakenDay = r.days_taken;
            result.TakenMonth = r.months_taken;
            result.TakenYear = r.years_taken;
            result.TakenComment = r.taken_comment;
            result.TakenPointDate = r.taken_point_day;
            result.TakenDateNext = r.taken_date_next;
            result.TakenDateLast = r.taken_date_last;
            result.Amount = amount;
            result.Comment = r.comment;
            result.SampleNumber = r.sample_number;
            result.Val01 = r.value_01;
            result.Val02 = r.value_02;
            result.Val03 = r.value_03;
            result.Val04 = r.value_04;
            result.Val05 = r.value_05;
            result.Val06 = r.value_06;
            result.Val07 = r.value_07;
            result.Val08 = r.value_08;
            result.Val09 = r.value_09;
            result.Val10 = r.value_10;
            result.Val11 = r.value_11;
            result.Val12 = r.value_12;
            result.DateExtraction = r.date_extraction;
            result.ReasonDelete = r.reason_delete;
            result.Status = r.status;

            return result;
        }



    }
}
